0% completed
Problem Statement
Given a root node of a binary tree and an integer target, delete all leaf nodes that have a value equal to target.
After removing such leaf nodes, if any of their parent nodes become leaf nodes with a value equal to target, they should also be removed. This process should continue until no more nodes can be deleted.
Examples
Example 1
- Input: root =
[1, 5, 3, 5, null, 5, 4], Target:5
- Expected Output:
[1,null,3,null,4] - Explanation: The left child of
5(left of root) is3and5. When we remove both leaf having value5, the left node of the root also becomes a leaf having a value equal to5. So, we also remove it.
Example 2
- Input: root =
[3, 3, 3, 3, 3, null, 4], Target:3
- Expected Output:
[3,null,3,null,4] - Explanation: Remove all
3except the root and right child of the root node.
Example 3
- Input: root =
[4, 5, 6, 5, null, 7, 8], Target:5
- Expected Output:
[4,null,6,7,8] - Explanation: The leaf nodes with value
5are removed first. The tree becomes[4, 5, 6, null, null, 7, 8]. Now, again remove the leaf node with the value5and the tree becomes [4, null, 6, 7, 8]`.
Constraints:
- The number of nodes in the tree is in the range [1, 3000].
- 1 <= Node.val, target <= 1000
Solution
To solve this problem, we use a recursive depth-first search (DFS) approach. We start from the root of the tree and recursively process the left and right subtrees to remove all leaf nodes with the given target value. During each recursive call, if a node becomes a leaf and its value matches the target, we remove it by returning null. This process continues until no target leaf nodes remain in the tree, ensuring that all eligible nodes are removed even if their parent nodes later become leaf nodes with the target value.
Step-by-Step Algorithm
-
Check if the Current Node is Null:
- If
rootisnull, returnnull. This handles cases where the tree or subtree is empty.
- If
-
Recursively Process the Left Subtree:
- Call
removeLeafNodes(root.left, target)to remove target leaf nodes from the left subtree. - Update
root.leftwith the returned value.
- Call
-
Recursively Process the Right Subtree:
- Call
removeLeafNodes(root.right, target)to remove target leaf nodes from the right subtree. - Update
root.rightwith the returned value.
- Call
-
Check if the Current Node is a Target Leaf:
- If
root.leftandroot.rightare bothnullandroot.valequals the target, it means the current node is a target leaf node. - Return
nullto remove the current node if it matches the target value and is a leaf.
- If
-
Return the Updated Node:
- If the current node is not a target leaf, return
rootto retain the node in the updated tree.
- If the current node is not a target leaf, return
Step-by-Step Algorithm Walkthrough
Given the input:
- Tree:
[1, 5, 3, 5, null, 5, 4] - Target:
5
-
Start at the Root Node
1:rootis1, callremoveLeafNodesonroot.left(node5).
-
Process Left Child
5of Root1:rootis5, callremoveLeafNodesonroot.left(node5).
-
Process Left Child
5of Node5:- It is leaf node and value is equal to target(5). So, return
null.
- It is leaf node and value is equal to target(5). So, return
-
Return to Node
5(Left Child of Root1):root.leftisnull.- Call
removeLeafNodesonroot.right(null) — returnsnull. rootis a target leaf (val = 5), returnnull.
-
Return to Root Node
1:root.leftis updated tonull.- Call
removeLeafNodesonroot.right(node3).
-
Process Right Child
3of Root1:rootis3, callremoveLeafNodesonroot.left(node5).
-
Process Left Child
5of Node3:- It is leaf node and value is equal to target(5). So, return
null.
- It is leaf node and value is equal to target(5). So, return
-
Return to Node
3:root.leftisnull.- Call
removeLeafNodesonroot.right(node4) — returnsnode 4.
-
Complete Processing Node
3:root.rightis4, not a target leaf, so return3.
-
Finish at Root Node
1:root.leftisnullandroot.rightis3. Return1as the final root.
Resulting tree after removal is: [1, null, 3, null, 4].
Code
Complexity Analysis
Time Complexity
The algorithm uses a depth-first search (DFS) to traverse the binary tree. In the worst case, the DFS will visit every node in the tree once. Thus, the time complexity is n is the total number of nodes in the binary tree.
Space Complexity
The space complexity is determined by the maximum depth of the recursion stack, which corresponds to the height of the binary tree. In the worst case, the height of the tree could be
On this page
Problem Statement
Examples
Solution
Step-by-Step Algorithm
Step-by-Step Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity